﻿using STMS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using STMS.Models.UIModels;
using STMS.Models.VModels;
using Communication.Modbus;
using System.IO.Ports;
using STMS.Models.DModels;

namespace STMS.BLL
{
    public class RegionTemperBLL
    {
        ViewSRegionTemperDAL vrtDAL = new ViewSRegionTemperDAL();
        StoreRegionDAL srDAL = new StoreRegionDAL();
        /// <summary>
        /// 查询仓库分区温度列表
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="temperState"></param>
        /// <returns></returns>
        public List<UCStoreBoxModel> GetStoreBoxList(int storeId, int temperState)
        {
            List<ViewSRegionTemperInfo> regionTemperList = vrtDAL.GetSRegionTemperList(storeId, temperState);
            return GetUCBoxList(regionTemperList);
        }

        /// <summary>
        /// 修改室内温度（升温或降温处理）
        /// </summary>
        /// <param name="regionId"></param>
        /// <param name="srTemper"></param>
        /// <returns></returns>
        public bool UpdateRegionTemperature(int regionId, decimal srTemper)
        {


            return srDAL.UpdateRegionTemperature(regionId, srTemper);
        }

        /// <summary>
        /// 读取仓库分区温度值并更新到数据库
        /// </summary>
        /// <returns></returns>
        public List<UCStoreBoxModel> ReadRegionTemperatures()
        {
            List<ViewSRegionTemperInfo> regionTemperList = vrtDAL.GetSRegionTemperList(0, -1);
            ModbusBase mb = new ModbusRTU("COM1", 9600, 8, StopBits.One, Parity.None);
            ushort count = (ushort)regionTemperList.Count;

            if (mb.Open())
            {
                regionTemperList= Task.Run(async () =>
                {
                    //通过相应方法获取数据
                    List<ushort> values = await mb.ReadUInt16(0, len: count);
                    if (values != null)
                    {
                        mb.Close();
                        if (values.Count > 0)
                        {
                            List<StoreRegionInfo> regions = new List<StoreRegionInfo>();
                            for (int i = 0; i < values.Count; i++)
                            {
                                decimal temper = decimal.Parse(((decimal)values[i] / 10).ToString("0.00"));
                                StoreRegionInfo info = new StoreRegionInfo();
                                info.SRegionId = regionTemperList[i].SRegionId;
                                info.SRTemperature = temper;
                                regionTemperList[i].SRTemperature = temper;
                                if (temper > 0)
                                {
                                    if (regionTemperList[i].AllowLowTemperature > temper)
                                        info.TemperState = 0;
                                    else if (regionTemperList[i].AllowHighTemperature < temper)
                                        info.TemperState = 2;
                                    else
                                        info.TemperState = 1;
                                    regionTemperList[i].TemperState = info.TemperState;
                                    regions.Add(info);
                                }
                            }

                            bool bl = srDAL.UpdateRegionsTemperatures(regions);
                        }
                        return regionTemperList;
                    }
                    else
                        return null;
                }).Result;
                mb.Close();
                return GetUCBoxList(regionTemperList);
            }

            return null;
        }

        private List<UCStoreBoxModel> GetUCBoxList(List<ViewSRegionTemperInfo> regionTemperList)
        {
            List<UCStoreBoxModel> reList = new List<UCStoreBoxModel>();
            if(regionTemperList!=null)
            {
                int id = 0;
                int i = 0;
                foreach (ViewSRegionTemperInfo info in regionTemperList)
                {
                    string range = info.AllowLowTemperature + "~" + info.AllowHighTemperature;
                    if (info.AllowLowTemperature == info.AllowHighTemperature && info.AllowLowTemperature == 0)
                        range = "无";
                    if (info.SRTemperature > 0)
                    {
                        if (info.AllowLowTemperature > info.SRTemperature)
                            info.TemperState = 0;
                        else if (info.AllowHighTemperature < info.SRTemperature)
                            info.TemperState = 2;
                        else
                            info.TemperState = 1;
                    }
                    i += 1;
                    if (i > 1)
                        id += 1;
                    reList.Add(new UCStoreBoxModel()
                    {
                        SRgionId = info.SRegionId,
                        SId = id,
                        SRegionName = info.SRegionName,
                        SRTemper = info.SRTemperature,
                        MaxTemper = info.AllowHighTemperature,
                        MinTemper = info.AllowLowTemperature,
                        TemperRange = range,
                        TemperState = info.TemperState,
                        ProductCount = info.TotalCount
                    });
                }
            }
            
            return reList;
        }
    }
}
